/*
 *@本js功能：学生在线考试
 *@本js作者：杨锡君
 *@编写时间：2017年11月24日
 */
$(function(){
	var Alert1 = new Alert(); 
	var confirm1 = new Confirm();
	var $testList = $(".testList");
	function testLiModel(opt){
		var str = "";
		if(opt.type == 1){
			 str = "<li style=\"cursor:not-allowed;\">";
		}else if(opt.type == 2){
			 str = "<li data-courseid="+ opt.courseId +" class=\"generate\">";
		}else{
			str = "<li style=\"cursor:not-allowed;\">";
		}
		if(opt.type == 2){
			str+="<div style=\"background: "+ opt.courseColer +"\">";
		}else{
			str+="<div style=\"background: "+ opt.courseColer +"\">";
		}
		str += "<i class=\"iconfont\">"+ opt.courseTitle +"</i>"
				+"<p title="+ opt.courseName   +" class=\"courseName\">"+ opt.courseName +"</p>";
								
		var str1 = "";
		if(opt.type == 1){
//			str1 = "<a class=\"generate\" data-courseid="+ opt.courseId +" data-coursename="+ opt.courseName +" href=\"javscript:;\">生成试题</a>";
			str1 = "<div class=\"btnBox noneData\"><div>暂无考试<div>";
		}else if(opt.type == 2){
			str1 = "<div class=\"btnBox\"><p title="+"教师:"+ opt.userName +">教师:"+ opt.userName +"</p><p title="+"时长:"+ opt.examtime+"分钟" +">时长:"+ opt.examtime +"分钟</p>";
		}else{
			str1 = "<div class=\"btnBox noneData\"><div>试卷已提交<div>";
		}
		var str2 = "</div></div>"
				+"</li>";
		return str + str1 + str2;
	}
	var stuOnlineExam = {
		init:function(){
			this.bindEvent();
		},
		bindEvent:function(){
			var self = this;
			
			self.getDataList();
		},
		getDataList:function(){
			var self = this;
			$.ajax({
				type:"post",
				url:"getAllStuCourse",
				async:true,
				success:function(msg){
					msg = JSON.parse(msg);
					if(msg.result == "success"){
                        if(msg.courseList.length!=0){
                            $testList.html("");
                            var list = "";
                            $.map(msg.courseList, function(opt) {
                                list+=testLiModel(opt);
                            });
                            $testList.html(list);
                            self.generateEvent();
                        }else{
                            var trImg = "<div><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></div>";
                            $testList.html(trImg)
                        }
					}else{
                        if(msg.failMsg != undefined){
                            if(msg.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }else{
                            if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                Alert1.content = msg.errorMsg;
                            } else {
                                Alert1.content = "获取失败";
                            }
                            Alert1.init();
                            Alert1.show();
                        }
					}

					
				}
			});
		},
		generateEvent:function(){
//			试题详情
			$(".generate").off("click").click(function(){
				var json = JSON.stringify({
					"courseId":Number($(this).attr("data-courseid"))
				});
				var that = $(this);
                $.ajax({
                    type:"post",
                    url:"stuOnlineExam",
                    async:true,
					data:{
                    	"jsonStr":json
					},
                    success:function(msg){
                        msg = JSON.parse(msg);
                        if(msg.flag == true){
                            var json = JSON.stringify({
                                "pageName":"stuOnlineExamDetail",
                                "paramData":{
                                    "courseId":Number(that.attr("data-courseid"))
                                }
                            });
                            window.location.href = "skipPage?jsonStr="+json;
						}else{
                            Alert1.content = msg.errMsg;
                            Alert1.init();
                            Alert1.show();
                            return false;
						}
                    }
                });

			});
		}
	};
	stuOnlineExam.init();
});
